package com.alibaba.otter.manager.web.resource;

import com.alibaba.otter.manager.biz.config.channel.ChannelService;
import com.alibaba.otter.manager.web.dto.RestResponse;
import com.alibaba.otter.shared.arbitrate.ArbitrateManageService;
import com.alibaba.otter.shared.common.model.config.channel.Channel;
import org.springframework.web.bind.annotation.*;

/**
 *
 */
@RequestMapping("/api/v1/switch-warm-up")
@RestController
public class SwitchWarmupResource {

    private final ChannelService channelService;

    private final ArbitrateManageService arbitrateManageService;

    public SwitchWarmupResource(ChannelService channelService, ArbitrateManageService arbitrateManageService) {
        this.channelService = channelService;
        this.arbitrateManageService = arbitrateManageService;
    }

    @PutMapping("/switch/{pipelineId}")
    public RestResponse<Void> doSwitch(@PathVariable("pipelineId") Long pipelineId) {
        Channel channel = channelService.findByPipelineId(pipelineId);
        arbitrateManageService.channelEvent().restart(channel.getId());// 尝试重新启动
        arbitrateManageService.systemEvent().switchWarmup(channel.getId(), pipelineId);
        return RestResponse.ok();
    }

    @PutMapping("/restart/{pipelineId}")
    public RestResponse<Void> restart(@PathVariable("pipelineId") Long pipelineId) {
        Channel channel = channelService.findByPipelineId(pipelineId);
        arbitrateManageService.channelEvent().restart(channel.getId());// 尝试重新启动
        channelService.notifyChannel(channel.getId());// 推送下配置
        return RestResponse.ok();
    }
}
